package com.cheng.channel;

import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * 自定义属性工具类
 * <p>
 * 注意： 宽高都为 match_content 且无实际内容时构造方法不会调用
 * 使用方法：
 * xxx extends Component
 * 获取自定义属性：
 * String count = AttrUtils.getStringFromAttr(attrSet,"cus_count","0");
 * <p>
 * 属性定义：
 * 布局头中加入  xmlns:hap="http://schemas.huawei.com/apk/res/ohos" 使用hap区分自定义属性与系统属性
 * 即可使用 hap:cus_count="2"  不加直接使用ohos:cus_count="2"
 *
 *
 * <p>
 */
public class AttrUtils {

    public final static String CHANNEL_HEIGHT = "channelHeight";
    public final static String CHANNEL_COLUMN = "channelColumn";
    public final static String CHANNEL_PADDING = "channelPadding";
    public final static String CHANNEL_VERTICAL_SPACING = "channelVerticalSpacing";
    public final static String CHANNEL_HORIZONTAL_SPACING = "channelHorizontalSpacing";
    public final static String CHANNEL_NORMAL_BACKGROUND = "channelNormalBackground";
    public final static String CHANNEL_EDIT_BACKGROUND = "channelEditBackground";
    public final static String CHANNEL_FOCUSED_BACKGROUND = "channelFocusedBackground";
    public final static String CHANNEL_NORMAL_TEXT_COLOR = "channelNormalTextColor";
    public final static String CHANNEL_FIXED_COUNT = "channelFixedCount";
    public final static String CHANNEL_FIXED_TEXT_COLOR = "channelFixedTextColor";
    public final static String CHANNEL_FIXED_BACKGROUND = "channelFixedBackground";
    public final static String CHANNEL_FOCUSED_TEXT_COLOR = "channelFocusedTextColor";
    public final static String CHANNEL_TEXT_SIZE = "channelTextSize";
    public final static String TIP_EDIT_BACKGROUND = "tipEditBackground";
    public final static String PLATES_TITLE_BACKGROUND = "platesTitleBackground";
    public final static String TIP_EDIT_TEXT_COLOR = "tipEditTextColor";
    public final static String PLATES_TITLE_COLOR = "platesTitleColor";
    public final static String PLATES_TITLE_BOLD = "platesTitleBold";
    public final static String TIP_EDIT_TEXT_SIZE = "tipEditTextSize";
    public final static String PLATES_TITLE_SIZE = "platesTitleSize";
    public final static String PLATES_TITLE_HEIGHT = "platesTitleHeight";
    public final static String PLATES_TITLE_LEFT_RIGHT_PADDING = "platesTitleLeftRightPadding";
    public final static String OTHER_SUBTITLE_BACKGROUND = "otherSubTitleBackground";
    public final static String OTHER_SUBTITLE_TEXT_COLOR = "otherSubTitleTextColor";
    public final static String OTHER_SUBTITLE_TEXT_SIZE = "otherSubTitleTextSize";
    public final static String SUBTITLE_BACKGROUND = "subTitleBackground";
    public final static String SUBTITLE_TEXT_COLOR = "subTitleTextColor";
    public final static String SUBTITLE_TEXT_SIZE = "subTitleTextSize";
    public final static String SUBTITLE_NAME = "subTitleName";
    public final static String OTHER_SUBTITLE_NAME = "otherSubTitleName";
    public final static String TIP_FINISH_BACKGROUND = "tipFinishBackground";
    public final static String TIP_FINISH_TEXT_COLOR = "tipFinishTextColor";
    public final static String TIP_FINISH_TEXT_SIZE = "tipFinishTextSize";


    private AttrUtils() {

    }

    /**
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return String
     */
    public static String getStringFromAttr(AttrSet attrSet, String name, String defaultValue) {
        String value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = attrSet.getAttr(name).get().getStringValue();
            }
        } catch (Exception e) {
            return value;
        }
        return value;
    }

    /**
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return Integer
     */
    public static Integer getDimensionFromAttr(AttrSet attrSet, String name, Integer defaultValue) {
        Integer value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = attrSet.getAttr(name).get().getDimensionValue();
            }
        } catch (Exception e) {
            return value;
        }
        return value;
    }

    /**
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return Integer
     */
    public static Integer getIntegerFromAttr(AttrSet attrSet, String name, Integer defaultValue) {
        Integer value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = attrSet.getAttr(name).get().getIntegerValue();
            }
        } catch (Exception e) {
            return value;
        }
        return value;
    }

    /**
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return float
     */
    public static float getFloatFromAttr(AttrSet attrSet, String name, float defaultValue) {
        float value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = attrSet.getAttr(name).get().getFloatValue();
            }
        } catch (Exception e) {
        }
        return value;
    }

    /**
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return Long
     */
    public static Long getLongFromAttr(AttrSet attrSet, String name, Long defaultValue) {
        Long value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = attrSet.getAttr(name).get().getLongValue();
            }
        } catch (Exception e) {
        }
        return value;
    }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultValue
         * @return int 颜色
         */
        public static int getColorFromAttr(AttrSet attrSet, String name, int defaultValue) {
            int value = defaultValue;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    value = attrSet.getAttr(name).get().getColorValue().getValue();

                }
            } catch (Exception e) {
                return value;
            }
            return value;
        }

    /**
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return boolean
     */
    public static boolean getBooleanFromAttr(AttrSet attrSet, String name, boolean defaultValue) {
        boolean value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = attrSet.getAttr(name).get().getBoolValue();
            }
        } catch (Exception e) {
            return value;
        }
        return value;
    }

    /**
     * @param attrSet
     * @param name
     * @param defaultElement
     * @return Element
     */
    public static Element getElementFromAttr(AttrSet attrSet, String name, Element defaultElement) {
        Element element = defaultElement;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                element = attrSet.getAttr(name).get().getElement();
            }
        } catch (Exception e) {
            return element;
        }
        return element;
    }


    /**
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return Integer
     */
    public static Integer getResourceIdFromAttr(AttrSet attrSet, String name, Integer defaultValue) {
        Integer value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = Integer.valueOf(attrSet.getAttr(name).get().getStringValue().replace("$layout:", ""));
            }
        } catch (Exception e) {
            return value;
        }
        return value;
    }

    public static int vpToPx(Context context, int dp) {
        int density = context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return Math.round(dp * density);
    }

}
